package com.itheima.mm.controller;

import com.itheima.mm.constant.JwtClaimsConstant;
import com.itheima.mm.dto.UserLoginDTO;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.entity.UserPageQueryParams;
import com.itheima.mm.pojo.User;
import com.itheima.mm.properties.JwtProperties;
import com.itheima.mm.service.UserService;
import com.itheima.mm.utils.JwtUtil;
import com.itheima.mm.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j//lombok注解，用于输出日志
@RequestMapping("/user")  //设置映射请求的父路径
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login.do")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("{请求登录请求，请求参数:{}",userLoginDTO);
       //获取登录对象，，从mapepr层与mysql对比获取的
        User userLogin = userService.login(userLoginDTO);

        //返回给前端
        //这里的userLoginVO为返回给前端的封装对象
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(userLogin.getId());

        //TODO 后面完善

        //登录令牌
        //定义载荷
        Map<String, Object> claims = new HashMap<>();
        //JwtClaimsConstant.USER_ID为一个常量即user_id
        claims.put(JwtClaimsConstant.USER_ID,userLogin.getId());
        //生成令牌
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),claims);


        userLoginVO.setToken(token);
        //System.out.println("token:"+token);


        return Result.success(userLoginVO);

    }
    @PostMapping("/findListAll.do")
    public Result<PageResult<User>> findListByPage(
            @RequestBody QueryPageBean<UserPageQueryParams> queryPageBean){
        log.info("执行分页列表操作：{}",queryPageBean);
        PageResult pageResult = userService.findListByPage(queryPageBean);
        return Result.success(pageResult);
    }
    @PostMapping("/add.do")
    public Result<User> add(@RequestBody User user){
        log.info("执行添加用户操作：{}",user);
        //调用service添加用户
        userService.add(user);
        //返回数据
        return Result.success();
    }
    @GetMapping("/findById.do")
    public Result<User> findById(Integer id){
        log.info("执行删除学科目录操作：{}",id);
        //调用service删除
        User user = userService.findById(id);
        //返回数据
        return Result.success(user);
    }
    @PostMapping("/update.do")
    public Result<User> update(@RequestBody User user){
        log.info("执行修改用户操作：{}",user);
        //调用service修改用户
        userService.update(user);
        //返回数据
        return Result.success(null,"修改成功");
    }

    @GetMapping("/delete.do")
    public Result delete(Integer userId){
        log.info("开始执行用户删除接口：{}",userId);

        //调用业务
        userService.deleteUser(userId);

        //返回数据
        return Result.success(null,"用户删除成功");
    }


}
